<template>
  <el-breadcrumb  class="breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item
          v-for="(item, index) in breadcrumbData"
          :key="item.path"
      >
        <!-- 不可点击项 -->
        <span v-if="index === breadcrumbData.length - 1" class="no-redirect">{{item.meta.title }}</span>
        <!-- 可点击项 -->
        <a v-else class="redirect" @click.prevent="onLinkClick(item)">{{item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>
<script setup>
import { ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router"

const route = useRoute()
// 生成数组数据
const breadcrumbData = ref([])
const getBreadcrumbData = () => {
  breadcrumbData.value = route.matched.filter(
      item => item.meta && item.meta.title
  )
}

watch(
    route,
    () => {
      getBreadcrumbData()
    },
    {
      immediate: true
    }
)

// 处理点击事件
const router = useRouter()
const onLinkClick = item => {
  console.log("当前路由"+item.path)
  router.push(item.path)
}

</script>
<style lang="scss">
.breadcrumb {
  display: inline-block;
  margin: 0 0 -11px 0;

  .redirect {
    color: #666;
    font-weight: 600;
    cursor: pointer;
  }

  ::v-deep(.no-redirect) {
    color: #97a8be;
    cursor: text;
  }
}
.breadcrumb-enter-active,
.breadcrumb-leave-active {
  transition: all 0.5s;
}

.breadcrumb-enter-from,
.breadcrumb-leave-active {
  opacity: 0;
  transform: translateX(20px);
}

.breadcrumb-leave-active {
  position: absolute;
}
</style>